package com.nd.mobile.study.view;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.util.L;
import com.nd.mobile.study.domain.VideoRecord;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class VideoRecordFragment  extends Fragment{

	private ListView mLV;
	private VideoRecordListAdapter mAdapter;
	private View mContainer;
	private LayoutInflater mInflater;
	
//	@Override
//	public void onAttach(Activity activity) {
//		super.onAttach(activity);
//		View v = LayoutInflater.from(activity).inflate(R.layout.simple_list, null, false); 
//		mLV = (ListView)v.findViewById(R.id.common_lv);
//		loadList(activity);
//	}
	
	
//	@Override
//	public void onActivityCreated(Bundle savedInstanceState) {
//		super.onActivityCreated(savedInstanceState);
//		FragmentActivity activity = getActivity();
//		mContainer = LayoutInflater.from(activity).inflate(R.layout.simple_list, null, false); 
//		mLV = (ListView)mContainer.findViewById(R.id.common_lv);
//		loadList(activity);
//	}
	
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		L.v(this.getClass(), "onCreate");
//	}
//	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		FragmentActivity activity = getActivity();
		mContainer = LayoutInflater.from(activity).inflate(R.layout.simple_list, null, false); 
		mLV = (ListView)mContainer.findViewById(R.id.common_lv);
		loadList(activity);
		return mContainer;
	}
	
//	@Override
//	public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
//		super.onInflate(activity, attrs, savedInstanceState);
//		loadList(activity);
//	}


	private void loadList(Activity activity){
		((BaseActivity)activity).getLoading().p();
		new GetVideoRecordTask(activity).execute();
	}
	
	private class GetVideoRecordTask extends SimpleAsyncTask<Void, Void, VideoRecord[]>{

		public GetVideoRecordTask(Context context) {
			super(context);
		}

		@Override
		protected VideoRecord[] onLoad(Void... params) throws Exception {
			return App.getInstance().getUser().getVideoRecord();
		}

		@Override
		protected void onUI(VideoRecord[] result) {
			mAdapter = new VideoRecordListAdapter(mContext, result);
			mLV.setAdapter(mAdapter);
			mAdapter.notifyDataSetChanged();
//			VideoRecordListAdapter adapter = new VideoRecordListAdapter(mContext, result);
//			View v = LayoutInflater.from(mContext).inflate(R.layout.simple_list, null, false); 
//			ListView lv = (ListView)v.findViewById(R.id.common_lv);
//			lv.setAdapter(adapter);
//			adapter.notifyDataSetChanged();
		}
		
	}
	
	private class VideoRecordListAdapter extends SimpleListAdapter<VideoRecord>{

		public VideoRecordListAdapter(Context context, List<VideoRecord> data) {
			super(context, data);
		}

		public VideoRecordListAdapter(Context context, VideoRecord[] data) {
			super(context, data);
		}

		private class ViewHolder {
			TextView	tvTitle;
			TextView	tvBeginTime;
			TextView	tvEndTime;
			TextView	tvLVTime;
			TextView	tvLookNum;
			TextView    tvLVTimeTotal;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(null == convertView){
				convertView = mInflater.inflate(R.layout.video_record_list_item, null);
				
				holder = new ViewHolder();
				holder.tvTitle = (TextView) convertView.findViewById(R.id.video_record_catalog_title);
				
				
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvTitle.setText(mData.get(position).getName());
			
			return convertView;
		}
		
	}

}
